<template>
  <div class="fullscreen-container">
    <div class="background-overlay"></div>
    <img
        class="background-image"
        src="@/assets/VCG211305627301.webp"
        alt="医药系统背景图"
    >
    <div class="login-content" :class="{ active: showContent }">
      <div class="title-container">
        <h1 class="title">糖尿病智能健康助理</h1>
        <p class="subtitle">高效 · 精准 · 安全</p>
      </div>
      <div class="login-form">
        <div class="form-group">
          <el-input
              v-model="username"
              placeholder="请输入用户名"
              clearable
              class="enlarged-input"
              prefix-icon="User"
          />
        </div>
        <div class="form-group">
          <el-input
              v-model="password"
              type="password"
              placeholder="请输入密码"
              show-password
              class="enlarged-input"
              prefix-icon="Lock"
          />
        </div>
        <el-button
            type="primary"
            round
            class="enlarged-button"
            @click="tiaozhuang"
        >
          登 录
        </el-button>
      </div>

      <!-- 注册入口 -->
      <div class="register-action">
        <span>还没有账号？</span>
        <el-link @click="goToRegister" class="register-link">立即注册</el-link>
      </div>

      <!-- 版权信息 -->
      <div class="copyright">
        © {{ currentYear }} 糖尿病智能健康助理 - 专注糖尿病管理
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from "vue-router";
import { ref, computed, onMounted } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";

const router = useRouter();
const username = ref("");
const password = ref("");
const currentYear = computed(() => new Date().getFullYear());
const showContent = ref(false)

const tiaozhuang = async () => {
  if (!username.value || !password.value) {
    ElMessage.error('用户名和密码不能为空');
    return;
  }

  try {
    // 使用代理路径
    const response = await axios.post('/api/login/', {
      username: username.value,
      password: password.value
    });

    // 检查200状态码
    if (response.status === 200) {
      // 保存token并跳转到主页面
      localStorage.setItem('token', response.data.access_token);
      ElMessage.success('登录成功');
      router.push('/MainFrame/homepage');
    } else {
      ElMessage.error(response.data.detail || '登录失败');
    }

  } catch (error) {
    if (error.response) {
      const { status, data } = error.response;

      if (status === 401) {
        ElMessage.error('用户名或密码错误');
      } else if (status === 422) {
        ElMessage.error('请求格式错误');
      } else {
        ElMessage.error('登录失败，请重试');
      }

      console.error(`API错误 (${status}):`, data);
    } else {
      ElMessage.error('网络连接失败');
      console.error('网络错误:', error);
    }
  }
}
const goToRegister = () => {
  router.push('/register');
}
onMounted(() => {
  // 组件挂载后立即显示内容（触发动画）
  setTimeout(() => {
    showContent.value = true
  }, 100) // 可选延迟，确保过渡效果触发
})
</script>

<style scoped>
.fullscreen-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
}

/* 背景图新样式 */
.background-image {
  position: absolute;
  width: 60%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  left: 0;
  z-index: 0;
  filter: brightness(0.8);
  clip-path: polygon(0 0, 100% 0, 90% 100%, 0 100%);
  transition: all 0.5s ease;
}

/* 鼠标悬停时图片效果 */
.background-image:hover {
  filter: brightness(0.9);
  clip-path: polygon(0 0, 100% 0, 85% 100%, 0 100%);
}

/* 调整遮罩层 */
.background-overlay {
  position: absolute;
  width: 60%;
  height: 100%;
  background: linear-gradient(90deg, rgba(0,0,0,0.3), rgba(0,0,0,0.1));
  z-index: 1;
  left: 0;
  pointer-events: none
}

/* 登录框布局优化 */
.login-content {
  position: absolute;
  right: 0;
  width: 40%;
  height: 100%;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: -20px 0 15px rgba(0, 0, 0, 0.1);
  padding: 0 40px;
  display: flex;
  flex-direction: column;
  justify-content: center; /* 垂直居中 */
  align-items: center;
  z-index: 2;
  opacity: 0;                /* 默认透明 */
  transform: translateX(30px); /* 初始位置向右偏移30px */
  transition: opacity 0.6s ease, transform 0.6s ease; /* 过渡效果 */
}

.login-content.active {
  opacity: 1;                /* 激活时完全不透明 */
  transform: translateX(0);  /* 回到原始位置 */
}

/* 内容居中显示 */
.title-container,
.login-form,
.register-action {
  max-width: 400px;
  width: 100%;
  text-align: center;
}

.title-container {
  margin-bottom: 40px;
}

.title {
  font-size: 36px;
  color: #303133;
  margin-bottom: 10px;
  font-weight: 600;
  letter-spacing: 2px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.1);
  transition: all 0.3s ease;
}

.title:hover {
  letter-spacing: 3px; /* 悬停时增加字母间距 */
}

.subtitle {
  font-size: 16px;
  color: #909399;
  letter-spacing: 1px;
}

.login-form {
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 25px;
  position: relative;
}

.enlarged-input {
  width: 100% !important;
}

.enlarged-input :deep(.el-input__wrapper) {
  height: 48px;
  padding: 0 15px 0 40px;
  border-radius: 24px;
  box-shadow: 0 0 0 1px #DCDFE6 inset;
  background: rgba(255,255,255,0.9);
  transition: all 0.3s ease;
}

.enlarged-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409EFF inset;
  background: #fff;
}

.enlarged-input :deep(.el-input__inner) {
  height: 100%;
  line-height: 1;
  font-size: 16px;
  color: #606266;
}

.enlarged-input :deep(.el-input__prefix) {
  left: 12px;
  display: flex;
  align-items: center;
}

.enlarged-input :deep(.el-input__prefix .el-icon) {
  font-size: 18px;
  color: #C0C4CC;
}

.enlarged-input :deep(.el-input__suffix) {
  right: 12px;
  display: flex;
  align-items: center;
}

.enlarged-button {
  width: 100% !important;
  height: 48px !important;
  font-size: 18px !important;
  border-radius: 24px !important;
  margin-top: 10px;
  letter-spacing: 2px;
  background: linear-gradient(90deg, #409EFF, #337ecc);
  border: none;
  transition: all 0.3s;
}

.enlarged-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 4px rgba(64, 158, 255, 0.3);
}

/* 注册入口样式 */
.register-action {
  display: flex;
  align-items: center;
  justify-content: center; /* 水平居中 */
  margin-top: 20px;
  font-size: 14px;
  color: #606266;
}

.register-link {
  color: #409EFF;
  margin-left: 5px;
  font-weight: 500;
  position: relative;
  padding-bottom: 2px;

  &:hover {
    color: #337ecc;
    text-decoration: none;

    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 1px;
      background-color: #409EFF;
    }
  }
}

/* 版权信息固定在底部 */
.copyright {
  position: absolute;
  bottom: 30px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 13px;
  color: #909399;
}
</style>
